/*     */ package com.zimbra.cs.mime;
/*     */ 
/*     */ import com.google.common.collect.HashMultimap;
/*     */ import com.google.common.collect.Multimap;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.FileUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.util.Collection;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TnefFileCache
/*     */ {
/*     */   private static TnefFileCache sInstance;
/*  43 */   private static final Log sLog = LogFactory.getLog(TnefFileCache.class);
/*     */   
/*     */ 
/*     */ 
/*     */   private File mCacheDir;
/*     */   
/*     */ 
/*     */ 
/*  51 */   private Multimap<Integer, String> mMessageToPartDigests = HashMultimap.create();
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*  56 */   private Map<String, File> mDigestToFile = new HashMap();
/*     */   
/*     */   private TnefFileCache(File cacheDir) {
/*  59 */     this.mCacheDir = cacheDir;
/*     */   }
/*     */   
/*     */   public static TnefFileCache getInstance() {
/*  63 */     if (sInstance == null) {
/*  64 */       throw new IllegalStateException(TnefFileCache.class.getSimpleName() + " has not been started");
/*     */     }
/*  66 */     return sInstance;
/*     */   }
/*     */   
/*     */   public static synchronized void startup() throws IOException
/*     */   {
/*  71 */     if (sInstance == null) {
/*  72 */       File cacheDir = new File(LC.zimbra_tmp_directory.value() + File.separator + "tnef");
/*  73 */       sLog.debug("Initializing TNEF cache in %s", new Object[] { cacheDir });
/*  74 */       FileUtil.deleteDir(cacheDir);
/*  75 */       FileUtil.ensureDirExists(cacheDir);
/*  76 */       sInstance = new TnefFileCache(cacheDir);
/*     */     }
/*     */   }
/*     */   
/*     */   public static synchronized void shutdown() {
/*  81 */     if (sInstance != null) {
/*     */       try {
/*  83 */         FileUtil.deleteDir(sInstance.mCacheDir);
/*     */       } catch (IOException e) {
/*  85 */         sLog.warn("Unable to delete TNEF cache directory %s.", sInstance.mCacheDir, e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public File getFile(MimeMessage msg, MimeBodyPart part) throws IOException, MessagingException
/*     */   {
/*  92 */     int msgHashcode = msg.hashCode();
/*  93 */     String digest = ByteUtil.getSHA1Digest(part.getInputStream(), true);
/*  94 */     sLog.debug("Getting file for MimeMessage %d, part digest %s.", new Object[] { Integer.valueOf(msgHashcode), digest });
/*     */     
/*  96 */     synchronized (this)
/*     */     {
/*  98 */       this.mMessageToPartDigests.put(Integer.valueOf(msgHashcode), digest);
/*     */       
/*     */ 
/*     */ 
/* 102 */       File file = (File)this.mDigestToFile.get(digest);
/* 103 */       if (file == null) {
/* 104 */         file = new File(this.mCacheDir, digest);
/* 105 */         sLog.debug("Saving TNEF content to %s.", new Object[] { file });
/* 106 */         part.saveFile(file);
/* 107 */         this.mDigestToFile.put(digest, file);
/*     */       }
/* 109 */       return file;
/*     */     }
/*     */   }
/*     */   
/*     */   public synchronized void purge(MimeMessage msg) {
/* 114 */     if (msg == null) {
/* 115 */       return;
/*     */     }
/* 117 */     int hashCode = msg.hashCode();
/* 118 */     sLog.debug("Purging message with hashcode %d.", new Object[] { Integer.valueOf(hashCode) });
/*     */     
/* 120 */     Collection<String> digests = this.mMessageToPartDigests.removeAll(Integer.valueOf(hashCode));
/* 121 */     Set<String> allDigests; if ((digests != null) && (!digests.isEmpty()))
/*     */     {
/*     */ 
/* 124 */       allDigests = getAllMessagePartDigests();
/* 125 */       for (String digest : digests) {
/* 126 */         if (!allDigests.contains(digest)) {
/* 127 */           File file = (File)this.mDigestToFile.remove(digest);
/* 128 */           sLog.debug("Deleting %s.", new Object[] { file });
/* 129 */           if (!file.delete()) {
/* 130 */             sLog.warn("Unable to delete %s.", new Object[] { file });
/*     */           }
/*     */         } else {
/* 133 */           sLog.debug("Not deleting file on disk because it is referenced by another message.");
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private synchronized Set<String> getAllMessagePartDigests()
/*     */   {
/* 143 */     Set<String> allDigests = new HashSet();
/* 144 */     for (Iterator i$ = this.mMessageToPartDigests.keySet().iterator(); i$.hasNext();) { int hashCode = ((Integer)i$.next()).intValue();
/* 145 */       allDigests.addAll(this.mMessageToPartDigests.get(Integer.valueOf(hashCode)));
/*     */     }
/* 147 */     return allDigests;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mime/TnefFileCache.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */